package com.itheima.controller;

import com.itheima.aspect.Log;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping
    public Result pageEmp(EmpQueryParam param){
        PageBean pageBean = empService.pageEmp(param);
        return Result.success(pageBean);
    }


    /**
     * 添加员工
     */
    @PostMapping
    @Log
    public Result addEmp(@RequestBody Emp emp){
        log.info("emp:{}",emp);
        empService.addEmp(emp);
        return Result.success();
    }


    /**
     * 删除员工
     */
    @DeleteMapping
    @Log
    public Result deleteByIds(@RequestParam List<Integer> ids){
        log.info("ids:{}",ids);
        empService.deleteByIds(ids);
        return Result.success();
    }


    /**
     * 回显员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getEmp(@PathVariable Integer id){
        Emp emp =  empService.getEmp(id);
        return Result.success(emp);
    }

    @PutMapping
    @Log
    public Result updateEmp(@RequestBody Emp emp){
        empService.updateEmp(emp);
        return Result.success();
    }

    /**
     *查询所有员工接口
     * @return
     */
    @GetMapping("/list")
    public Result getEmps(){
        List<Emp> empList =  empService.getEmps();
        return Result.success(empList);
    }
}
